import { ref } from 'vue'
import { defineStore } from 'pinia'

export const useUserStore = defineStore('user', () => {
  // token
  const token = ref('')
  // 用户信息
  const userInfo = ref({})
  // 持仓信息
  const userPositionInfo = ref({})

  // getter
  function getToken() {
    return this.token
  }

  function getUserInfo() {
    return this.userInfo
  }

  function getUserPositionInfo() {
    return this.userPositionInfo
  }

  function setToken(token) {
    this.token = token
  }
  function setUserInfo(user) {
    this.userInfo = user
  }

  function setUserPositionInfo(position) {
    this.userPositionInfo = position
  }

  function clearUserInfo() {
    this.userInfo = {}
    this.token = ''
    this.userPositionInfo = {}
    localStorage.clear()
  }

  return {
    token,
    userInfo,
    userPositionInfo,
    getToken,
    getUserInfo,
    getUserPositionInfo,
    setToken,
    setUserInfo,
    setUserPositionInfo,
    clearUserInfo
  }
}, {
  persistent: {
    enabled: true,
    storage: localStorage
  }
})
